package br.com.imagecropper.servlet;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.imagecropper.cropper.Cropper;

/**
 * Servlet implementation class ImageCropper
 */
@WebServlet("/ImageCropperServlet")
public class ImageCropper extends HttpServlet {
    private static final long serialVersionUID = -5283625402226985869L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // Recuperando a imagem (Pensar em um esquema melhor, pq isso aqui esta funcionando
        // porque a imagem esta junto do projeto)
        String img_source = request.getParameter("imageSource");
        String imgPath = getServletContext().getRealPath("/") + img_source;
        BufferedImage img = ImageIO.read(new File(imgPath));

        Cropper cropper = new Cropper();
        BufferedImage cropped = cropper.getCroppedImage(this, request, img);

        LoadImage.img = cropped;
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
